//
//  BIModel.m
//  BIProjectModule
//
//  Created by yown on 2022/2/18.
//

#import "BIModel.h"

#import <BIToolModule/BIUserDefault.h>

@implementation BIModel

#pragma mark - Archive/UnArchive

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [self modelEncodeWithCoder:aCoder];
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    return [self modelInitWithCoder:aDecoder];
}

- (id)copyWithZone:(NSZone *)zone {
    return [self modelCopy];
}

- (NSUInteger)hash {
    return [self modelHash];
}

- (BOOL)isEqual:(id)object {
    return [self modelIsEqual:object];
}

- (NSString *)description {
    return [self modelDescription];
}

#pragma mark - Save/Load/Remove

+ (instancetype)loadFromLocal {
    NSData *userData = [[BIUserDefault sharedInstance] objectForKey:NSStringFromClass([self class])];
    return userData ? [self unarchiveData:userData] : nil;
}

- (void)saveSelfToLocal {
    NSData *userData = [self archiveData];
    if (userData) {
        [[BIUserDefault sharedInstance] setObject:userData key:NSStringFromClass([self class])];
    }
}

- (void)removeSelfFromLocal {
    [[BIUserDefault sharedInstance] setObject:nil key:NSStringFromClass([self class])];
}

- (NSData *)archiveData {
    return [NSKeyedArchiver archivedDataWithRootObject:self];
}

+ (instancetype)unarchiveData:(NSData *)data {
    return [NSKeyedUnarchiver unarchiveObjectWithData:data];
}

@end
